package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:ahang
 * @create:2022-09-29 8:44
 * @Description:
 */
@Api(tags = "平台属性接口")
@RestController
@RequestMapping("admin/product/")
public class BaseAttrController {

    @Autowired
    private BaseAttrInfoService baseAttrInfoService;

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    /**
     * 根据三级分类id查询所有的值
     * @param c1id
     * @param c2id
     * @param c3id
     * @return
     */
    @GetMapping("attrInfoList/{c1id}/{c2id}/{c3id}")
    public Result getAttr(@PathVariable("c1id") Long c1id,
                        @PathVariable("c2id") Long c2id,
                        @PathVariable("c3id") Long c3id){

        List<BaseAttrInfo> infos = baseAttrInfoService.getBaseAttrInfoAndValue(c1id,c2id,c3id);
        return Result.ok(infos);
    }

    /**
     * 添加/修改平台属性
     * 有id就修改,没有id新增
     * @return
     */
    @PostMapping("saveAttrInfo")
    public Result insertAttr(@RequestBody BaseAttrInfo baseAttrInfo){
        if(baseAttrInfo.getId() == null){
            //新增
            baseAttrInfoService.insertAttr(baseAttrInfo);
        }else{
            //修改
            baseAttrInfoService.updateAttr(baseAttrInfo);
        }
        return Result.ok();
    }

    /**
     * 根据平台属性ID获取平台属性对象数据
     * @param attrId
     * @return
     */
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){
        List<BaseAttrValue> result = baseAttrValueService.getAttrValueList(attrId);
        return Result.ok(result);
    }
}
